package cn.sytton.taffecloud.serivce.auth.security.handler;

import cn.sytton.taffecloud.common.base.constants.ExceptionMessage;
import cn.sytton.taffecloud.common.base.response.R;
import cn.sytton.taffecloud.common.base.util.JsonUtil;
import cn.sytton.taffecloud.common.servlet.util.ResponseWriter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;

/**
 * 认证入口点
 *
 * @author skyrock
 */
@Slf4j
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint, Serializable {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) {
        ResponseWriter.print(response, HttpStatus.UNAUTHORIZED.value(), JsonUtil.getString(R.fail(ExceptionMessage.UNAUTHORIZED)));
    }
}
